package string_question.leecode.test97;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/8 8:27
 */
public class Solution1 {

    public boolean isInterleave(String s1, String s2, String s3) {
        if (s1.length() + s2.length() != s3.length()) return false;
        int c1 = 0, c2 = 0, c3 = 0;
        return helper(s1, s2, s3, c1, c2, c3);
    }

    private boolean helper(String s1, String s2, String s3, int c1, int c2, int c3) {
        if (c1 == s1.length()) {
            return s2.substring(c2, s2.length()).equals(s3.substring(c3, s3.length()));
        }
        if (c2 == s2.length()) {
            return s1.substring(c1, s1.length()).equals(s3.substring(c3, s3.length()));
        }
        if (s1.charAt(c1) == s3.charAt(c3)) {
            if (helper(s1, s2, s3, c1 + 1, c2, c3 + 1)) return true;
        }
        if (s2.charAt(c2) == s3.charAt(c3)) {
            if (helper(s1, s2, s3, c1, c2 + 1, c3 + 1)) return true;
        }
        return false;
    }
}
